package cn.backpackerxl.easyJDBC.main;

import cn.backpackerxl.easyJDBC.util.EasyJDBCUtil;
import cn.backpackerxl.easyJDBC.util.Typeof;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: backpackerxl
 * CreateTime: 2022/5/13
 * FileName: Executor
 **/
public class Executor {

    private static Connection cn = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    private Executor() {
    }


    /**
     * 只包含基础查询
     *
     * @param sql 语句
     */
    public static void doExecutorSQLUtil(String sql) {
        try {
            cn = EasyJDBCUtil.getConnection();
            ps = cn.prepareStatement(sql);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            EasyJDBCUtil.close(cn, ps);
        }
    }

    /**
     * 用于<b>增<b/> <b>删</b> <b>改</b>基础查询
     *
     * @param sql 语句
     * @param ie  回调函数，描述操作行为
     * @return 操作结果
     */
    public static int doExecutorByMRS(String sql, IExecutorPlus ie) {
        try {
            cn = EasyJDBCUtil.getConnection();
            ps = cn.prepareStatement(sql);
            ie.doExe(ps);
            return ps.executeUpdate();
        } catch (SQLException | IllegalAccessException e) {
            e.printStackTrace();
            return -1;
        } finally {
            EasyJDBCUtil.close(cn, ps);
        }
    }

    /**
     * 用于<b>查<b/> 的基础查询
     *
     * @param sql 语句
     * @param ie  回调函数，对查询结果进行进一步加工
     */
    public static void doExecutorSQLUtil(String sql, IExecutor ie) {
        try {
            cn = EasyJDBCUtil.getConnection();
            ps = cn.prepareStatement(sql);
            rs = ps.executeQuery();
            ie.doExe(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            EasyJDBCUtil.close(cn, ps, rs);
        }
    }

    /**
     * 用于<b>查<b/> 的基础查询
     *
     * @param sql  语句
     * @param plus 回调函数，描述操作行为
     * @param ie   回调函数，对查询结果进行进一步加工
     */
    public static void doExecutorSQLUtil(String sql, IExecutorPlus plus, IExecutor ie) {
        try {
            cn = EasyJDBCUtil.getConnection();
            ps = cn.prepareStatement(sql);
            plus.doExe(ps);
            rs = ps.executeQuery();
            ie.doExe(rs);
        } catch (SQLException | IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            EasyJDBCUtil.close(cn, ps, rs);
        }
    }

    /**
     * 用于<b>查<b/> 的基础查询
     *
     * @param sql  语句
     * @param plus 回调函数，描述操作行为
     * @param ie   回调函数，对查询结果进行进一步加工
     * @param <E>  泛型对象
     * @return 封装对象
     */
    public static <E> E doExecutorSQLSelect(String sql, IExecutorPlus plus, IExecutorSelect ie) {
        try {
            cn = EasyJDBCUtil.getConnection();
            ps = cn.prepareStatement(sql);
            plus.doExe(ps);
            rs = ps.executeQuery();
            return ie.doExe(rs);
        } catch (SQLException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        } finally {
            EasyJDBCUtil.close(cn, ps, rs);
        }
    }

    /**
     * 设置值
     *
     * @param o     对象
     * @param value 回调函数
     */
    public static void doSetFieldValue(Object o, IValue value) {
        if (o != null) {
            try {
                value.doSetValue();
            } catch (SQLException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
